package com.jy.modules.cims.settle.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.contract.service.ContractService;
import com.jy.modules.cims.creditright.service.SecondBuyCreditService;
import com.jy.modules.cims.data.common.dto.TCMmCreditPackageDTO;
import com.jy.modules.cims.data.dto.ContractAllDto;
import com.jy.modules.cims.data.dto.CreditDetailDto;
import com.jy.modules.cims.data.dto.DealDto;
import com.jy.modules.cims.deal.service.DealService;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.core.message.PageParameter;
import com.jy.platform.core.message.PageUtil;
import com.jy.platform.core.message.QueryReqBean;
import com.jy.platform.restservice.exception.AbaboonException;
import com.jy.platform.restservice.web.base.BaseController;

/**
 * 结算处理控制层
 */
@Controller
@RequestMapping("/creditBuy")
public class CreditBuyController extends BaseController {
	private static final String JSP_URL = "cims/creditManage/buy/";

	/**
	 * 结算处理控制层
	 */
	public CreditBuyController() {
	}

	/**
     * 
     */
	@Autowired
	@Qualifier("com.jy.modules.cims.deal.service.DealService")
	public DealService dealService;

	@Autowired
	@Qualifier("com.jy.modules.cims.contract.service.ContractService")
	private ContractService contractService;

	@Autowired
	@Qualifier("com.jy.modules.cims.creditright.service.SecondBuyCreditService")
	private SecondBuyCreditService secondBuyCreditService;
	
	@Autowired
    @Qualifier("com.jy.modules.cims.contract.service.ContractService")
    private ContractService serviceContract;

	/**
	 * 通过 Controller 请求 跳转 进入至相关 页面
	 */
	@RequestMapping(value = "/prepareExecute/{operate}")
	public ModelAndView execute(@PathVariable("operate") String operate) throws AbaboonException {
		ModelAndView model = new ModelAndView();
		if ("toQueryPage".equals(operate)) {// 跳转至 查询页面
			model.setViewName(JSP_URL + "queryCredit");
		} else if ("toAdd".equals(operate)) { // 跳转至 新增页面
			String id = this.getParameterString("id");
			String contractCode = this.getParameterString("contractCode");
			ContractAllDto contractAllDto = contractService.getContractAllInfo(contractCode);
			model.addObject("id", id);
			model.addObject("contractAllDto", contractAllDto);
			model.setViewName(JSP_URL + "addCreditBuy");
		} else if ("toDeal".equals(operate)) {
			ContractAllDto contractAllInfo = contractService.getContractAllInfo(this.getParameterString("contractCode"));
			//String billDate = this.getParameterString("billDate");
			//String creditPackageName = this.getParameterString("creditPackageName");
			String settleDealCodes = this.getParameterString("settleDealCodes");
			String planSellSum = this.getParameterString("planSellSum");
			String practicalSellSum = this.getParameterString("practicalSellSum");
			String cooperorgCode = this.getParameterString("cooperorgCode");
			model.addObject("dto", contractAllInfo);
			model.addObject("contractAllDto", contractAllInfo);
			model.addObject("settleDealCodes", settleDealCodes);
			model.addObject("cooperorgCode",cooperorgCode);
			//model.addObject("billDate", billDate);
			//model.addObject("creditPackageName", creditPackageName);
			model.addObject("planSellSum", planSellSum);
			model.addObject("practicalSellSum", practicalSellSum);
			model.setViewName(JSP_URL + "addCooperorgDeal");
		} else if ("toUpdate".equals(operate)) {
			String contractCode = this.getParameterString("contractCode");
			String dealCode = this.getParameterString("dealCode");
			String cooperorgCode = this.getParameterString("cooperorgCode");
			String creditPackageName = this.getParameterString("creditPackageName");
			String id = this.getParameterString("id");
			model = contractService.queryAllDTO(cooperorgCode, contractCode, dealCode);
			List<TCMmCreditPackageDTO> list = serviceContract.dealPackages(cooperorgCode,contractCode,creditPackageName);
        	model.addObject("packageDto", CollectionsUtils.isEmpty(list)? null : list.get(0));
        	//获取标的日期
        	List<CreditDetailDto> listCredit = dealService.getCreditDeliverDateByDealCode(dealCode);
        	model.addObject("creditDeliverDate", CollectionsUtils.isEmpty(listCredit)? null : listCredit.get(0));
			model.addObject("id", id);
			model.setViewName(JSP_URL + "updateTCMmCooperorgDeal");
		}else if ("toAddCreditBuy".equals(operate)) {
			model.setViewName(JSP_URL + "addCreditDealDetailBuy");
		}else if("toBackDeal".equals(operate)){
			String cooperorgCode = this.getParameterString("cooperorgCode");
			String settleDealCodes = this.getParameterString("settleDealCodes");
			String planSellSum = this.getParameterString("planSellSum");
			String practicalSellSum = this.getParameterString("practicalSellSum");
			model.addObject("cooperorgCode",cooperorgCode);
			model.addObject("settleDealCodes", settleDealCodes);
			model.addObject("planSellSum", planSellSum);
			model.addObject("practicalSellSum", practicalSellSum);
			model.setViewName(JSP_URL + "addCooperorgDeal");
		}

		return model;
	}

	/**
	 * 结算处理
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public DataMsg settle(HttpServletRequest request, HttpServletResponse response) {
		// TODO implement here
		return null;
	}

	/**
	 * 试算处理
	 * 
	 * @param request
	 * @param response
	 * @param result
	 */
	public void trial(HttpServletRequest request, HttpServletResponse response, DataMsg result) {
		// TODO implement here
	}

	/**
	 * 
	 * @Title save
	 * @Description 保存交易
	 * @param request
	 * @param response
	 * @param result
	 *            void
	 * @author lizhiqiang
	 * @date 2015-1-20-上午9:45:29
	 * @update
	 * @throws
	 */
	@RequestMapping(value = "/testSettleCooperorgDeal")
	@ResponseBody
	public DataMsg testSettleCooperorgDeal(HttpServletRequest request, DealDto dto, @ModelAttribute DataMsg dataMsg) {
		try {
			dto = (DealDto) super.initDto(dto);
			// dealService.updateCreditDealDetailToCreditDetail(dto);

			dataMsg = super.initDataMsg(dataMsg);
			dataMsg.setMsg("试算完成！");
			dataMsg.setData(this.makeJSONData(dto.getId()));
		} catch (Exception e) {
			dataMsg.failed(e.getMessage());
			logger.error("执行方法insertSysResource异常：", e);
		}
		return dataMsg;
	}

	/**
	 * 
	 * @Title save
	 * @Description 分页查询所有类型结算流水
	 * @param request
	 * @param response
	 * @param result
	 *            void
	 * @author lizhiqiang
	 * @date 2015-1-20-上午9:45:29
	 * @update
	 * @throws
	 */
	@RequestMapping(value = "/queryAllTypeSettle")
	@ResponseBody
	public DataMsg queryAllTypeSettle(HttpServletRequest request, DealDto dto, @ModelAttribute DataMsg dataMsg) {
		try {
			dataMsg = super.initDataMsg(dataMsg);
			dto = (DealDto) super.initDto(dto);
			if (SimpleStringUtils.hasText(dto.getCooperorgCode())) {
				Map<String, Object> searchParams = SearchParamUtil.getSearchParam(dto);
				QueryReqBean params = new QueryReqBean();
				params.setSearchParams(searchParams);
				PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
				params.setPageParameter(pageInfo);
				List<DealDto> list = secondBuyCreditService.searchDealForCreditBuyByPaging(params.getSearchParams());
				dataMsg.setData(list);
				dataMsg.setTotalRows(pageInfo.getTotalCount());
			}
		} catch (Exception e) {
			dataMsg.failed(e.getMessage());
		}
		return dataMsg;
	}
}